<?php
/*
	字辈
*/
class ZibeiController  extends BaseController
{
	public function indexAction()
	{
		//$this->load();
		
		parent::load([
			'title'  => '字辈',
			'css'    => 'index',
			'js'     => 'index'
		]);
		
		//$this->debug('select * from net_user where user_code="'.$this->infoUser('user_code').'"');
		$genno = isset($_GET['genno'])?$_GET['genno']:null;
		$GET = $genno?'?genno='.$genno:'';
		$nav = '<a href="/TeClan/Zibei/crtzibei'.$GET.'">新增</a>';
		$zbList = '';
		if($genno){
			$center = Center::FindFirst('gen_no="'.$genno.'"');
			$cZibei = $center->zibei;		
			$minstzb = $this->tag->select(array(
					'minstzb',
					Zibei::find('gen_no="'.$genno.'"'),
					"using"=>array("zibei_no","zibei"),
					"useEmpty" => true,
					'emptyText' => '请选择最小字辈',
					'id' => 'minstzb'
			));			
			if($cZibei){
				//$this->debug(unserialize($cZibei));
				$minstzb = '<p><a href="javascript:void(0);" id="reZbBtn">重排序</a><span id="zb2centBar" style="display:none;"><a href="javascript:void(0);" id="zb2center" genno="'.$genno.'">顺序生成</a>'.$minstzb.'</span></p>';
				$zbList = $minstzb.'<div id="zibei_list"><p>字辈排序于：'.$center->zibei_crttm.'</p><ol class="zibei">'.implode('',unserialize($cZibei)).'</ol></div>';
			}else{
				$minstzb = '<p id="zb2centBar"><a href="javascript:void(0);" id="zb2center" genno="'.$genno.'">顺序生成</a>'.$minstzb.'</p>';
				$zbData = Zibei::find('gen_no="'.$genno.'"');
				foreach($zbData as $v){
					$zbList .= '<li><a href="/TeClan/Zibei/crtzibei?zbno='.$v->zibei_no.'">'.$v->zibei.'</a>('.$v->pinyin.')<span class="auther">'.$v->user_name.'</span><span class="date">'.$v->mtime.'</span></li>';
				}
				//字辈信息以及操作按钮
				if($zbList){					
					$zbList = $minstzb.'
						<div id="zibei_list">
							<ol class="zibei">'.$zbList.'</ol>
						</div>
					';
				} 
			}
			
		}
		$this->view->setVars(array(
			'nav'=>$nav,
			'zibeiList'=>$zbList
		));
		
		
	}
	/*提交表单-创建字辈*/
	public function crtzibeiAction()
	{
		parent::load([
			'title'=>'创建字辈',
			'js'=>'crtzibei',
			'css'=>'crtzibei'
		]);
		if($this->request->isPost()){
			$zibei_no = $this->request->getPost('zibei_no');
			
			if($zibei_no){//数据修改
				$this->flash->error('您正在修改数据！');
				//$this->debug($zb);
				$zb = Zibei::findFirst('zibei_no="'.$zibei_no.'"');

				$zb->zibei = $this->request->getPost('zibei');
				$zb->pinyin = $this->request->getPost('pinyin');
				$zb->pzibei = $this->request->getPost('pzibei');
				$zb->parent_no = $this->request->getPost('parent_no');
				$zb->gen_no = $this->request->getPost('gen_no');
				$zb->remark = $this->request->getPost('remark');

				if($zb->update() == false){
					$this->flash->error('数据修改失败');
					foreach ($zb->getMessages() as $message) {
						echo $message->getMessage(), "<br/>";
					}
					return false;
				}
				header('Location:/TeClan/Zibei/index?genno='.$zb->gen_no);
			}else{
				$genno = isset($_GET['genno'])? $_GET['genno']:null;
				$uInfo = $this->infoUser();
				$zibei = $this->request->getPost('zibei');
				$pinyin = $this->request->getPost('pinyin');

				$zb = new Zibei();
				$zb->zibei = $zibei;
				$zb->pinyin = $pinyin;
				$zb->user_code = $uInfo['user_code'];
				$zb->user_name = $uInfo['user_name'];
				$zb->remark = $this->request->getPost('remark');
				$zb->parent_no = $this->request->getPost('parent_no');
				$zb->pzibei = $this->request->getPost('pzibei');
				$zb->gen_no = $this->request->getPost('gen_no');
				if ($zb->save() == false) {
					$this->flash->error('数据提交失败！');
					foreach ($zb->getMessages() as $message) {
						echo $message->getMessage(), "<br/>";
					}
					return false;
				}else{
					//$this->flash->error('家谱创建成功！');
					//return $this->forward('Zibei/index?genno='.$zb->gen_no);
					//return $this->forward('Zibei/index?genno='.$zb->gen_no);
					header('Location:/TeClan/Zibei/index?genno='.$zb->gen_no);
				}
			}
			
		}
	}
	//参数数据
	public function delzbAction()
	{
		isset($_GET['zbno']) or die('数据请求失败！');
		$zbno = $_GET['zbno'];
		$zb = Zibei::findFirst('zibei_no="'.$zbno.'"');
		$genno = $zb->gen_no;
		if($zb->delete() == false){
			foreach ($zb->getMessages() as $message) {
				echo $message, "\n";
			}
		}else{
			header('Location:/TeClan/Zibei/index?genno='.$genno);
		}
	}
	public function ajaxAction()
	{
		$ret = '';
		$data = ajax();
		$item = isset($data['item'])? $data['item']:null;
		if($item == 'zb2center'){
			$genno = $data['genno'];
			$zbno = isset($data['minstzb'])? $data['minstzb']:null;
			$center = Center::findFirst('gen_no="'.$genno.'"');
			$zibei = $center->zibei;
			$log = $center->editlog;
			if((empty($zibei) && !empty($data['minstzb'])) || isset($data['resetzb'])){
				//'gen_no="'.$genno.'"',
				/*		
				$zb = Zibei::findFirst(array(					
					'zibei_no="'.$zbno.'"'
				));
				$this->debug($zbno);
				*/
				$list = $this->zibei2order($zbno);
				krsort($list);//rsort		
				$data = array(
					'list' 	=> $list,
					'mtime'	=> date('Y-m-d H:i:s')
				);
				$log .= '<p style="margin:0px;padding:0px;">用户于'.date('Y-m-d H:i:s').'更新了字辈顺序，操作ip来源'.getIp().'</p>';
				$center->zibei = serialize($list);
				$center->zibei_crttm = date('Y-m-d H:i:s');
				$center->lately = date('Y-m-d H:i:s');
				$center->editlog = $log; 
				$center->save();
			}else{
				$data = array(
					'list' 	=> unserialize($zibei),
					'mtime'	=> $center->zibei_crttm
				);
			}
			//echo'<pre>';var_dump($data['list']);die;
			$ret = '<p>字辈排序于：'.$data['mtime'].'</p><ol class="zibei">'.implode($data['list']).'</ol>';
		}
		die($ret);
	}
	//2016年7月22日 顺序生成 : $serNo 初始第一个序号
	public function zibei2order($zbno,$serNo=1)
	{
		$data = array();
		$v = Zibei::findFirst('zibei_no="'.$zbno.'"');
		$data[] = '<li><a href="/TeClan/Zibei/crtzibei?zbno='.$v->zibei_no.'">'.$v->zibei.'</a>('.$v->pinyin.')<span class="auther">'.$v->user_name.'</span><span class="date">'.$v->mtime.'</span></li>';
		$v->ser_no = $serNo;
		$v->update();
		$cldzb = $v->parent_no;		
		if($cldzb != '0'){
			$serNo += 1;
			$getData = $this->zibei2order($cldzb,$serNo);
			if(is_array($getData)) $data = array_merge($data,$getData);
			else return '';
		}		
		return $data;
	}
	public function testAction()
	{
		//echo '<pre>';
		//print_r($this->di);
		//print_r($this->db);//配置文件
		//$cars = $this->modelsManager->executeQuery("SELECT * FROM net_user where user_code=\"346185011\"");
		//print_r($cars);
		
		$db = $this->getDbObj();
		$data = $db->fetchAll('select * from net_user where user_code="346185011"');
		//$data->setFetchMode(Phalcon\Db::FETCH_ASSOC);
		//$data->setFetchMode(Phalcon\Db::FETCH_ASSOC);
		echo '<pre>';
		print_r($data);
		//$this->query('select * from net_user where user_code="346185011"');
		print_r($db->fetchOne('select * from net_user where user_code="346185011"'));
		print_r($db->fetchAll('select * from net_user where user_code="346185011"'));
	}
}
